package com.shanghaionstar.nav.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.map.common.DriveMode;
import com.onstar.cn.sqlite.entities.RouteHistory;
import com.onstar.cn.map.common.PoiItemInfo;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.NavChooseInMapActivity;
import com.shanghaionstar.nav.NavRoutePlanFragment;
import com.shanghaionstar.nav.NavRoutePlanResultFragment;
import com.shanghaionstar.nav.iview.INavRoutePlanView;
import com.shanghaionstar.nav.model.MapChoosePoiType;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.sqlite.RouteHistoryImpl;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NavHolder;

import java.util.List;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NavRoutePlanPresenter extends BasePresenter {
    private AbstractMapActivity mapActivity;

    private PoiItemInfo routeStartPoiItemInfo, routeEndPoiItemInfo;
    private RouteHistoryImpl impl;
    private List<RouteHistory> routeHistoryList;// 起终点路径规划历史list
    private boolean isAddRouteHistoryList = true;

    public static final int ROUTEPLAN = 1;
    public static final int STARTLOCATION = 2;
    public static final int ENDLOCATION = 3;
    private FragmentManager fragmentManager;


    public NavRoutePlanPresenter(IMVPScheduler mvpScheduler, AbstractMapActivity mapActivity) {
        super(mvpScheduler);
        this.mapActivity = mapActivity;
        fragmentManager = mapActivity.getSupportFragmentManager();
    }


    //    public void back() {
//        BusinessUtil.sendReportByClickType(mapActivity,
//                FunctionNameFindId.Routeplanning_return.getStringValue());
//        back();
//    }


    public void rightSearchByMode(String routeStart, String routeEnd, int driverMode) {
        search(routeStart, routeEnd,driverMode);
    }

    public void rightSearch(String routeStart, String routeEnd) {
        BusinessUtil.sendReportByClickType(mapActivity,
                FunctionNameFindId.Routeplanning_searchicon.getStringValue());
        rightSearchByMode(routeStart, routeEnd, DriveMode.DrivingFastRoad);
    }

    private void search(String routeStart, String routeEnd,int driverMode) {
//        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.RouteButton.getStringValue());
        if (TextUtils.isEmpty(routeStart) || TextUtils.isEmpty(routeEnd)) {
            AlertDialogUtil.showAlertDialog(mapActivity, R.string.poi_route_title_str,
                    R.string.poi_route_not_search_not_condition);
            return;
        }
        if (!CommonUtil.checkNetWorkState(mapActivity)) {
            AlertDialogUtil.showAlertDialog(mapActivity, R.string.poi_route_title_str,
                    R.string.poi_networkerror_notsearch);
            return;
        }
        if (routeStart.equals(routeEnd)) {
            AlertDialogUtil.showAlertDialog(mapActivity, mapActivity.getString(R.string.dialog_warning),
                    mapActivity.getString(R.string.route_search_startpoint_endpoint_not_same));
            return;
        }
        if (!CommonUtil.isNull(routeStartPoiItemInfo) && !CommonUtil.isNull(routeEndPoiItemInfo)) {
            impl.saveRouteHistory(routeStartPoiItemInfo, routeEndPoiItemInfo);
        }
        NavRoutePlanResultFragment fragment = new NavRoutePlanResultFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("routeStartPoiItemInfo", routeStartPoiItemInfo);
        bundle.putSerializable("routeEndPoiItemInfo", routeEndPoiItemInfo);
//        bundle.putInt("driverMode", driverMode);
        fragment.setArguments(bundle);
        if (mapActivity != null) {
            mapActivity.addFragment(fragmentManager, fragment, true);
            mapActivity.showFragment(fragmentManager, NavRoutePlanResultFragment.class, NavRoutePlanFragment.class);
//            ((BaseFragmentActivity) mapActivity).changeFragment(((BaseFragmentActivity) mapActivity).getSupportFragmentManager(), fragment,
//                    NavRoutePlanResultFragment.class, false);
        }
    }

    public void onItemClick(int position) {
        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Routeplanning_routehistory.getStringValue());
        RouteHistory routeinfo = routeHistoryList.get(position);
        if (null == routeStartPoiItemInfo)
            routeStartPoiItemInfo = new PoiItemInfo();
        routeStartPoiItemInfo.setTitle(routeinfo.getStartPoiName());
        routeStartPoiItemInfo.setAddress(routeinfo.getStartPoiAdress());
        routeStartPoiItemInfo.setLatitude(routeinfo.getStartPoiLat());
        routeStartPoiItemInfo.setLongitude(routeinfo.getStartPoiLon());

        if (null == routeEndPoiItemInfo)
            routeEndPoiItemInfo = new PoiItemInfo();
        routeEndPoiItemInfo.setTitle(routeinfo.getEndPoiName());
        routeEndPoiItemInfo.setAddress(routeinfo.getEndPoiAdress());
        routeEndPoiItemInfo.setLatitude(routeinfo.getEndPoiLat());
        routeEndPoiItemInfo.setLongitude(routeinfo.getEndPoiLon());
//        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.RouteButton.getStringValue());
//        PoiLocationInfoInstance.getInstance().setRouteSearchStatus(RouteSearchStatusType.InProcess);
        searchRouteResult(false);
    }

    public void searchRouteResult(boolean isAddRouteHistoryList) {
        if (!CommonUtil.checkNetWorkState(mapActivity)) {
            AlertDialogUtil.showAlertDialog(mapActivity, R.string.poi_route_title_str,
                    R.string.poi_networkerror_notsearch);
            return;
        }
        if (routeStartPoiItemInfo.getLatitude() == routeEndPoiItemInfo.getLatitude()
                && routeStartPoiItemInfo.getLongitude() == routeEndPoiItemInfo.getLongitude()) {
            AlertDialogUtil.showAlertDialog(mapActivity, mapActivity.getString(R.string.dialog_warning),
                    mapActivity.getString(R.string.route_search_startpoint_endpoint_not_same));
//            PoiLocationInfoInstance.getInstance().setRouteSearchStatus(RouteSearchStatusType.Completed);
            return;
        }
//        PoiLocationInfoInstance.getInstance().setRouteSearchStatus(RouteSearchStatusType.InProcess);
//        NavRoutePlanResultFragment fragment = NavRoutePlanResultFragment.newInstance(routeStartPoiItemInfo,routeEndPoiItemInfo)
        mapActivity.addFragment(fragmentManager, NavRoutePlanResultFragment.newInstance(routeStartPoiItemInfo, routeEndPoiItemInfo), true);
        mapActivity.showFragment(fragmentManager, NavRoutePlanResultFragment.class, NavRoutePlanFragment.class);
        mapActivity.getNavDataHolder().setStartPoi(routeStartPoiItemInfo);
        mapActivity.getNavDataHolder().setEndPoi(routeEndPoiItemInfo);
//            ((BaseFragmentActivity) mapActivity).changeFragment(((BaseFragmentActivity) mapActivity).getSupportFragmentManager(), fragment,
//                    NavRoutePlanResultFragment.class, false);
        if (isAddRouteHistoryList) {
            impl.saveRouteHistory(routeStartPoiItemInfo, routeEndPoiItemInfo);
        }
    }

    /**
     * 初始化调用
     *
     * @param start
     * @param
     */
    public void setStartPoi(PoiItemInfo start) {
        routeStartPoiItemInfo = start;
        if (!CommonUtil.isNull(routeStartPoiItemInfo) && CommonUtil.isNull(routeStartPoiItemInfo.getCityName())) {
            routeStartPoiItemInfo.setCityName(NavHolder.getInstance(mapActivity).getCurrentCity());
        }
    }

    public void switchStartEndPoi() {
        PoiItemInfo tmp = routeStartPoiItemInfo;
        routeStartPoiItemInfo = routeEndPoiItemInfo;
        routeEndPoiItemInfo = tmp;
    }

    public void setEndPoi(PoiItemInfo end) {
        routeEndPoiItemInfo = end;
        if (!CommonUtil.isNull(routeEndPoiItemInfo) && CommonUtil.isNull(routeEndPoiItemInfo.getCityName())) {
            routeEndPoiItemInfo.setCityName(NavHolder.getInstance(mapActivity).getCurrentCity());
        }
    }

    public void initData() {
//        // 设置起始点所在城市
//        if (mapActivity instanceof NavActivity) {
//            routeStartPoiItemInfo = mapActivity.getNavDataHolder().getCurrentMapPoi();
//        }
////        routeStartPoiItemInfo = PoiLocationInfoInstance.getInstance().getStartPoiItemInfo();
//        if (!CommonUtil.isNull(routeStartPoiItemInfo) && CommonUtil.isNull(routeStartPoiItemInfo.getCityName())) {
//            routeStartPoiItemInfo.setCityName(NavHolder.getInstance(mapActivity).getCurrentCity());
//        }
//
//        // 设置终点所在城市
//        if (mapActivity instanceof NavActivity) {
//            routeEndPoiItemInfo = ((NavActivity) mapActivity).getMapHandler().getEndPoiItemInfo();
//        }
//        if (!CommonUtil.isNull(routeEndPoiItemInfo) && CommonUtil.isNull(routeEndPoiItemInfo.getCityName())) {
//            routeEndPoiItemInfo.setCityName(NavHolder.getInstance(mapActivity).getCurrentCity());
//        }
        String startTitle = "", endTitle = "";
        if (routeStartPoiItemInfo != null) {
            startTitle = routeStartPoiItemInfo.getTitle();
        }

        if (routeEndPoiItemInfo != null) {
            endTitle = routeEndPoiItemInfo.getTitle();
        }

        if (view instanceof INavRoutePlanView) {
            ((INavRoutePlanView) view).setText(startTitle, endTitle);
        }
    }

    public void getRouteHistory() {
        impl = new RouteHistoryImpl(mapActivity);
        routeHistoryList = impl.getRouteList();
        if (view instanceof INavRoutePlanView) {
            ((INavRoutePlanView) view).refreshView(routeHistoryList);
        }
    }


    public void startClick() {
        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Routeplanning_startpointfield.getStringValue());
//        NavSetAddressFragment navSetAddressFragment = NavSetAddressFragment.newInstance(MapChoosePoiType.START_POI);
//        NavChoosePointInMapFragment navChoosePointInMapFragment = NavChoosePointInMapFragment.newInstance(MapChoosePoiType.START_POI);
//        mapActivity.addFragment(fragmentManager, navChoosePointInMapFragment, true);
//        mapActivity.addFragment(fragmentManager, navSetAddressFragment, true);
//        mapActivity.showFragment(fragmentManager, NavSetAddressFragment.class, NavRoutePlanFragment.class);

        Intent intent = new Intent(mapActivity, NavChooseInMapActivity.class);
        intent.putExtra("mapChoosePoiType",MapChoosePoiType.START_POI);
        mapActivity.startActivityForResult(intent,1);
    }

    public void switchClick() {
        BusinessUtil.sendReportByClickType(mapActivity,
                FunctionNameFindId.Routeplanning_switchicon.getStringValue());
        // 切换名称
        if (view instanceof INavRoutePlanView) {
            ((INavRoutePlanView) view).switchTxt();
        }
        // 切换POI
//        PoiItemInfo tmp;
//        tmp = routeStartPoiItemInfo;
//        routeStartPoiItemInfo = routeEndPoiItemInfo;
//        routeEndPoiItemInfo = tmp;
//        PoiItemInfo routeStartPoiItemInfoTemp = routeStartPoiItemInfo;
//        PoiItemInfo routeEndPoiItemInfoTemp = routeEndPoiItemInfo;
//        routeStartPoiItemInfo = routeEndPoiItemInfoTemp;
//        routeEndPoiItemInfo = routeStartPoiItemInfoTemp;
    }

    public void endClick() {
        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Routeplanning_endpointfield.getStringValue());
//        NavSetAddressFragment navSetAddressFragment = NavSetAddressFragment.newInstance(MapChoosePoiType.END_POI);
//        NavChoosePointInMapFragment navChoosePointInMapFragment = NavChoosePointInMapFragment.newInstance(MapChoosePoiType.END_POI);
//        mapActivity.addFragment(fragmentManager, navChoosePointInMapFragment, true);
//        mapActivity.addFragment(fragmentManager, navSetAddressFragment, true);
//        mapActivity.showFragment(fragmentManager, NavSetAddressFragment.class, NavRoutePlanFragment.class);

        Intent intent = new Intent(mapActivity, NavChooseInMapActivity.class);
        intent.putExtra("mapChoosePoiType",MapChoosePoiType.END_POI);
        mapActivity.startActivityForResult(intent,1);
    }

    public void clear() {
        impl.deleteAll();
        routeHistoryList.clear();
    }

    public void onDestroy() {
        routeEndPoiItemInfo = new PoiItemInfo();
        routeStartPoiItemInfo = new PoiItemInfo();
//        PoiLocationInfoInstance.getInstance().setStartPoiItemInfo(null);
//        PoiLocationInfoInstance.getInstance().setEndPoiItemInfo(null);
    }


}
